<?php
//4578 资格赛  4400 大师赛  4579 锦标赛
include_once('../global.php');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = safe_replace($_POST['name']);
    $phone = safe_replace($_POST['phone']);
    //首先判断是否球员
    $sql = "SELECT * FROM match_player mp left join `player` p on mp.mp_player_id=p.pl_id WHERE p.pl_tele= '" . $phone . "' and p.pl_cn_name='" . $name . "' and mp.mp_club=567 ORDER BY p.pl_id desc";
    $res = $db->SelectSQL($sql);
    if ($res) { //球员
        $user_id = $res[0]['pl_id'];
        $type = 1; //球员
    }else{
        //判断英文
        $sql = "SELECT * FROM match_player mp left join `player` p on mp.mp_player_id=p.pl_id WHERE p.pl_tele= '".$phone."' and p.pl_en_name='".$name."' and mp.mp_club=567";
        $res = $db->SelectSQL($sql);
        if($res) { //球员英文
            $user_id = $res[0]['pl_id'];
            $type = 1;//球员
        }else{
            //特殊
            $sql = "SELECT * FROM `bmw_member_special` WHERE phone = '".$phone."' and name='".$name."'";
            $res = $db->SelectSQL($sql);
            if($res){ //如果存在直接登录
                $user_id=$res[0]['user_id'];
                $type = 1;//球员
            }else {
                //普通会员
                $type = 2;//会员
                $sql = "SELECT * FROM `bmw_member` WHERE phone = '" . $phone . "' and name='" . $name . "'";
                $res = $db->SelectSQL($sql);
                if ($res) { //如果存在直接登录
                    $user_id = $res[0]['user_id'];
                } else {
                    //注册
                    $act_datetime = date("Y-m-d H:i:s");
                    //入库
                    $sql = " insert into bmw_member(name,phone,create_time) values('{$name}','{$phone}','{$act_datetime}');";
                    $user_id = $db->ExecuteSQL($sql, 1);
                }
            }
        }



    }



    $_SESSION['user_id']=$user_id;
    $_SESSION['type']=$type;
    $redirectUrl = $_SESSION['redirect_url'];
    if (strpos($redirectUrl, 'login') !== false) {
        $redirectUrl = "https://mapp.scoringchina.com/bmwjunior/mobile/matchevent.php?mt_id=4164&cls=7";
    }
    unset($_SESSION['redirect_url']); // 使用后清除
    if (empty($redirectUrl)) {
        $redirectUrl = "https://mapp.scoringchina.com/bmwjunior/mobile/main.php";
    }
    echo json_encode(['code' => 1, 're_url' => $redirectUrl]);
    exit;
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
	  <link href="<?php echo CSS_PATH ?>index.css" rel="stylesheet" type="text/css">
    <title>登录页面</title>
    <style>
        body {
            margin: 0;
            font-family: pangmen;
            background: linear-gradient(180deg, #000418, #041a32, #196f64);
            color: white;
            text-align: center;
            position: relative;
            height: 100vh;
        }

        .icon {
            margin-top: 50px;
        }

        .icon img {
            width: 350px;
            height: 80px;
        }

        .description {
            margin: 20px;
            font-size: 18px;
        }

        .checkbox-container input {
            margin-right: 10px;
        }

        .checkbox-container a {
            color: #00aaff;
            text-decoration: none;
            cursor: pointer;
        }

        .login-button {
            background-color: #00aaff;
            color: white;
            border: none;
            border-radius: 5px;
            padding: 10px 20px;
            font-size: 18px;
            cursor: pointer;
        }

        .login-button:active {
            background-color: #0088cc;
        }

        /* 遮罩样式 */
        .overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.6);
            z-index: 999;
            display: none;
            /* 初始隐藏 */
        }

        /* 遮罩激活时显示 */
        .overlay.active {
            display: block;
        }

        /* 修正弹窗初始隐藏样式 */
        .popup {
            position: fixed;
            bottom: -100%;
            left: 0;
            width: 100%;
            background: #f5f5f5;
            color: #333;
            box-shadow: 0px -2px 10px rgba(0, 0, 0, 0.2);
            transition: bottom 0.3s ease-in-out;
            border-top-left-radius: 20px;
            border-top-right-radius: 20px;
            z-index: 1000;
            display: none;
            /* 完全隐藏 */
        }

        /* 激活时显示 */
        .popup.active {
            bottom: 0;
            display: block;
        }

        .popup-header {
            padding: 15px;
            font-size: 20px;
            font-weight: bold;
            color: #444;
            border-bottom: 1px solid #ddd;
        }

        .popup-body {
            padding: 20px;
        }

        .popup-body input {
            width: calc(100% - 20px);
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 8px;
            font-size: 16px;
            text-align: center;
            outline: none;
        }

        .popup-body input::placeholder {
            font-style: italic;
            color: #999;
        }

        .popup-body button {
            width: 100%;
            padding: 12px;
            background-color: #00aaff;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 18px;
            cursor: pointer;
        }

        .popup-body button:active {
            background-color: #0088cc;
        }

        /* 隐私政策弹窗 */
        .policy-popup .policy-content {
            max-height: 400px;
            overflow-y: auto;
            margin-bottom: 20px;
            text-align: left;
            font-size: 14px;
            line-height: 1.6;
            color: #444;
        }
    </style>
</head>

<body>
    <div class="icon">
        <img src="./images/000.png" alt="BMW Logo">
    </div>

    <div class="description" style="display:flex;flex-direction:row;align-items:center;justify-content:center;">
        <input type="checkbox" id="agreement" checked style="margin-left:10%">
        <label for="agreement">我已阅读并同意
            <a onclick="showPolicyPopup()">《隐私政策》</a>
        </label>
    </div>

    <button class="login-button" onclick="showPopup()">一键快捷登录</button>

    <div class="overlay" id="overlay" onclick="closePopup()"></div>

    <div class="popup" id="popup">
        <div class="popup-header">登录</div>
        <div class="popup-body">
            <input type="text" id="name" placeholder="请输入姓名">
            <input type="text" id="phone" placeholder="请输入手机号">
            <button onclick="login()">登录</button>
        </div>
    </div>

    <!-- 隐私政策弹窗 -->
    <div class="overlay" id="policyOverlay" onclick="closePolicyPopup()"></div>

    <div class="popup policy-popup" id="policyPopup">
        <div class="popup-header">隐私政策</div>
        <div class="popup-body">
            <div class="policy-content">
                <p><font class="poppin">BMW Entities Privacy Policy</font></p>
                <p>宝马中国及华晨宝马（“我们”）非常重视个人信息的保护。我们将出于<font class="poppin">2024 BMW</font>青少年高尔夫赛事报名、接待工作目的收集和使用您的姓名、手机号码、身份证号码（如需）以上个人信息。我们将在实现所述目的所必需的期间内处理您的个人信息，并使用符合业界标准的安全防护措施保护您的个人信息。您可以通过宝马中国官方网站随时查阅《宝马个人信息保护政策》。您参与活动、接受我们的服务并向我们提供前述个人信息即视为您已阅读并接受《宝马个人信息保护政策》。我们将按照相关法律法规及该政策合法使用和保护您的个人信息，您可根据《宝马个人信息保护政策》中第五章的内容行使您的个人信息权利。</p>
                <p><font class="poppin">BMW China and BMW Brilliance (“We”) attaches great importance to the protection of personal information. We will only collect and use your personal information for BMW GOLF NEXTGEN 2024 event registration and providing reception services. We will process your personal information such as name, mobile phone number, ID number (if needed) within the period necessary to achieve the stated purpose and protect your personal information in accordance with the standard security measures of the industry. You can look up the BMW Personal Information Protection Policy at any time via the official website. You are deemed to have read and accepted the BMW Personal Information Protection Policy by participating in the activities, receiving services from us and providing us with the aforementioned personal information. We will legally use and protect your personal information according to relevant laws and regulations which you can exercise your personal information rights in accordance with Chapter 5 of BMW Personal Information Protection Policy.</font></p>
            </div>
            <button onclick="closePolicyPopup()">关闭</button>
        </div>
    </div>

    <script src="css/mydata/jquery.min.js"></script>
    <script>
        function isPhoneNumber(phoneNumber) {
            return /^\d+$/.test(phoneNumber);  // 正则表达式匹配纯数字
           // var regex = /^[1][3-9]\d{9}$/;
           // return regex.test(phoneNumber);
        }

        function showAgreement(title) {
            alert(`这里显示的是 ${title} 的具体内容`);
        }

        function showPopup() {
            document.getElementById('popup').classList.add('active');
            document.getElementById('overlay').style.display = 'block';
        }

        function closePopup() {
            document.getElementById('popup').classList.remove('active');
            document.getElementById('overlay').style.display = 'none';
        }

        function showPolicyPopup() {
            const policyPopup = document.getElementById('policyPopup');
            const policyOverlay = document.getElementById('policyOverlay');
            policyPopup.classList.add('active');
            policyOverlay.classList.add('active');
        }

        function closePolicyPopup() {
            const policyPopup = document.getElementById('policyPopup');
            const policyOverlay = document.getElementById('policyOverlay');
            policyPopup.classList.remove('active');
            policyOverlay.classList.remove('active');
        }

        function login() {
            const name = document.getElementById('name').value;
            const phone = document.getElementById('phone').value;
            if (!name || !phone) {
                alert('请输入完整信息');
                return;
            }

            if (!isPhoneNumber(phone)) {
                alert('请填写正确的手机号');
                return;
            }

            $.ajax({
                type: "POST",
                url: "login.php",
                data: {
                    name: name,
                    phone: phone
                },
                dataType: "json",
                success: function(res) {
                    if (res.code == 1) {
                        closePopup();
                        window.location.href = res.re_url;
                    } else {
                        alert('登录失败，请重试');
                    }
                }
            });
        }
    </script>
</body>

</html>